import { excludeParseEventKeyList, excludeParseEventValueList } from './../enums/eventEnum'
import Image_404 from '@/assets/images/exception/image-404.png'
import {useDesignStore} from "@/store/modules/designStore/designStore";

// JSON序列化，支持函数和 undefined
export const JSONStringify = <T>(data: T) => {
  return JSON.stringify(
    data,
    (key, val) => {
      // 处理函数丢失
      if (typeof val === 'function') {
        return `${val}`
      }
      // 处理undefined丢失问题
      if (typeof val === 'undefined') {
        return null
      }
      return val
    },
    2
  )
}

export const evalFn = (fn: string) => {
  const Fun = Function // 一个变量指向Function，防止前端编译工具报错
  return new Fun('return ' + fn)
}

//  JSON反序列化，支持函数和 undefined
export const JSONParse = (data: string) => {
  return JSON.parse(data, (k, v) => {
    // 过滤函数字符串
    if (excludeParseEventKeyList.includes(k)) return v
    // 过滤函数值表达式
    if (typeof v === 'string') {
      const someValue = excludeParseEventValueList.some(excludeValue => v.indexOf(excludeValue) > -1)
      if (someValue) return v
    }
    // 还原函数值
    if (typeof v === 'string' && v.indexOf && (v.indexOf('function') > -1 || v.indexOf('>=') > -1)) {
      return evalFn(`(function() {return ${v}})()`)
    } else if (typeof v === 'string' && v.indexOf && v.indexOf('return ') > -1) {
      const baseLeftIndex = v.indexOf('(')
      if (baseLeftIndex > -1) {
        const newFn = `function ${v.substring(baseLeftIndex)}`
        return evalFn(`(function() {return ${newFn}})()`)
      }
    }

    return v
  })
}

// 错误图片
export const requireErrorImg = () => {
  return Image_404
}

// 修改主题色
export const setHtmlTheme = (themeName?: string) => {
  const e = window.document.documentElement
  if(themeName) {
    e.setAttribute('data-theme', themeName)
    return
  }
  const designStore = useDesignStore()
  e.setAttribute('data-theme', designStore.themeName)
}